﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.SessionState;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;
namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for EmployeeManagementwithDetails
    /// </summary>
    public class EmployeeManagementwithDetails : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string json = new StreamReader(context.Request.InputStream).ReadToEnd();

            var js = new JavaScriptSerializer();
            var emp  = js.Deserialize<OTS_EMPLOYEE>(json);
            emp.OTS_EDUCATION = GetEDU();
            emp.OTS_FAMILY = GetFamily();
            emp.OTS_MARITAL_STATUS = GetMaritalStatus();
            emp.OTS_EMERGENCY_NOTIFY = GetEmergencyNotify();

            var empservice = new  Employee_Service(emp);
            empservice.Add();
        }

        private List<OLTS.MODEL.OTS_EDUCATION> GetEDU()
        {
            var edu = new List<OLTS.MODEL.OTS_EDUCATION>();
            var eduSession = (List<OLTS.MODEL.Custom_Model.OTS_EDUCATION>)HttpContext.Current.Session["edu"];
            foreach (var d in eduSession.ToList())
            {
                if (d.Action == ActionEnum.Create)
                {
                    var model = new OLTS.MODEL.OTS_EDUCATION();

                    model.UNIVERSITY = d.UNIVERSITY;
                    model.YEAROFEDU = d.YEAROFEDU;
                    model.MAJOR = d.MAJOR;
                    model.CERTIFICATION = d.CERTIFICATION;
                    edu.Add(model);
                }
                
            }
            return edu;
        }

        private List<OLTS.MODEL.OTS_FAMILY> GetFamily()
        {
            var famList = new List<OLTS.MODEL.OTS_FAMILY >();
            var fam = (List<OLTS.MODEL.Custom_Model.OTS_FAMILY>)HttpContext.Current.Session["fam"];
            foreach (var d in fam.ToList())
            {
                if (d.Action == ActionEnum.Create)
                {
                    var model = new OLTS.MODEL.OTS_FAMILY();

                    model.NAMEOFFATHER = d.NAMEOFFATHER;
                    model.F_OCCUPATION = d.F_OCCUPATION;
                    model.NAMEOFMOTHER = d.NAMEOFMOTHER;
                    model.M_OCCUPATION = d.M_OCCUPATION;
                    model.NUMOFBRO_SIS = d.NUMOFBRO_SIS;
                    famList.Add(model);
                }

            }
            return famList;
        }
        private List<OLTS.MODEL.OTS_MARITAL_STATUS> GetMaritalStatus()
        {
            var marList = new List<OLTS.MODEL.OTS_MARITAL_STATUS>();
            var mar = (List<OLTS.MODEL.Custom_Model.OTS_MARITAL_STATUS>)HttpContext.Current.Session["mar"];
            foreach (var d in mar.ToList())
            {
                if (d.Action == ActionEnum.Create)
                {
                    var model = new OLTS.MODEL.OTS_MARITAL_STATUS();

                    model.SPOUSE_NAME = d.SPOUSE_NAME;
                    model.OCCUPATION = d.OCCUPATION;
                    model.NUMBEROFCHILD = d.NUMBEROFCHILD;
                    marList.Add(model);
                }

            }
            return marList;
        }

        private List<OLTS.MODEL.OTS_EMERGENCY_NOTIFY> GetEmergencyNotify()
        {
            var emerList = new List<OLTS.MODEL.OTS_EMERGENCY_NOTIFY>();
            var emr = (List<OLTS.MODEL.Custom_Model.OTS_EMERGENCY_NOTIFY>)HttpContext.Current.Session["emer"];
            foreach (var d in emr.ToList())
            {
                if (d.Action == ActionEnum.Create)
                {
                    var model = new OLTS.MODEL.OTS_EMERGENCY_NOTIFY();

                    model.NAME = d.NAME;
                    model.RELATIONSHIP = d.RELATIONSHIP;
                    model.ADDRESS = d.ADDRESS;
                    model.MOBILE = d.MOBILE;
                    model.HOME_TEL = d.HOME_TEL;
                    emerList.Add(model);
                }

            }
            return emerList;
        } 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}